package com.atcumt.bytedance;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

/**
 * 公司食堂
 * 输入：
 * 1
 * 5
 * 01102
 * 6
 * MFMMFF
 *
 * 输出：
 * 2
 * 1
 * 1
 * 3
 * 4
 * 4
 */
public class Main04 {

    public static void main(String[] args) throws IOException {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

        int T = Integer.parseInt(reader.readLine());
        for (int i = 0; i < T; i++) {
            int N = Integer.parseInt(reader.readLine());
            String tables = reader.readLine();
            int M = Integer.parseInt(reader.readLine());
            String enters = reader.readLine();

            int[] res = solve(tables, enters);
            for (int e : res) {
                writer.write(Integer.toString(e));
                writer.newLine();
            }
        }
        writer.flush();
    }

    private static int[] solve(String tables, String enters) {
        List<PriorityQueue<Integer>> queues = new ArrayList<>(3);
        queues.add(new PriorityQueue<>());
        queues.add(new PriorityQueue<>());
        queues.add(new PriorityQueue<>());

        for (int i = 0; i < tables.length(); i++) {
            queues.get(tables.charAt(i) - '0').add(i);
        }
        int n = enters.length();
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {

            int table;

            if (enters.charAt(i) == 'M') {
                if (queues.get(1).isEmpty()) {
                    table = queues.get(0).poll();
                    queues.get(1).add(table);
                } else {
                    table = queues.get(1).poll();
                    queues.get(2).add(table);
                }
            } else {
                if (queues.get(0).isEmpty()) {
                    table = queues.get(1).poll();
                    queues.get(2).add(table);
                } else {
                    table = queues.get(0).poll();
                    queues.get(1).add(table);
                }
            }
            res[i] = table + 1;
        }

        return res;
    }
}
